package com.fang;

import com.fang.task.ChildTask;
import lombok.SneakyThrows;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Fang Ruichuan
 * @date 2022-09-30 16:55
 */

public class LoopTest {
    private List<ChildTask> childTasks;

    public void initLoopTask() {
        childTasks = new LinkedList<>();
        childTasks.add(new ChildTask("childTask1"));
        childTasks.add(new ChildTask("childTask2"));
        childTasks.forEach(task -> new Thread(task::doExecute).start());
    }

    public void shutdownLoopTask() {
        if (!CollectionUtils.isEmpty(childTasks)) {
            childTasks.forEach(ChildTask::terminal);
        }
    }

    @SneakyThrows
    public static void main(String[] args) {
        LoopTest loopTest = new LoopTest();
        loopTest.initLoopTask();
        TimeUnit.MILLISECONDS.sleep(5000);
        loopTest.shutdownLoopTask();
    }
}
